package com.hundsun.bontal.hbi.hbi.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import com.hundsun.bontal.hbi.hbi.service.IMSUnderwritingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName IMSUnderwritingServiceImpl
 * @Description TODO
 * @Author wdl
 * @Date 2022/3/30 17:25
 * @Version
 */
@Component
@Slf4j
public class IMSUnderwritingServiceImpl implements IMSUnderwritingService {

    @Value("${upload.path}")
    private String uploadPath;

    private final String FILE_NAME = "fileName";

    private final String FILE_ID = "fileId";

    private final String RUNG = "-";

    public static final Map<String, MultipartFile> fileMap = new HashMap<>(10);

    @Override
    public List uploadFile(MultipartFile multipartFile, boolean isStore) throws IOException {
        Map<String, String> result = new HashMap<>();
        if (isStore) {
            String fileName = storeFileInDisk(multipartFile);
            result.put(FILE_NAME,fileName);
            log.info("存储文件到本地磁盘{}", fileName);
        }
        String fileId = storFileInMap(multipartFile);
        result.put(FILE_ID, fileId);
        log.info("存储文件到内存{}", fileId);
        List<Map<String, String>> pathList = new ArrayList<>();
        pathList.add(result);
        return pathList;
    }


    @Override
    public String storeFileInDisk(MultipartFile multipartFile) throws IOException {
        String yyyyMMdd = uploadPath + File.separator + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + File.separator;
        if (!FileUtil.exist(yyyyMMdd)) {
            FileUtil.mkdir(yyyyMMdd);
        }
        String fileName = UUID.randomUUID().toString() + "@" + multipartFile.getOriginalFilename();
        FileUtil.writeBytes(multipartFile.getBytes(), yyyyMMdd + fileName);
        return fileName;
//        File file1 = FileUtil.writeBytes(multipartFile.getBytes(), yyyyMMdd + fileName);
//        List<Map<String, String>> pathList = new ArrayList<>();
//        if (file1.length()>0){
//            String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
//            Map<String, String> map = new HashMap<>();
//            map.put("fileName", fileName);
//            map.put("suffix", suffix);
//            map.put("path", yyyyMMdd.replaceAll("\\\\","/"));
//            pathList.add(map);
//        }
    }

    @Override
    public String storFileInMap(MultipartFile multipartFile) {
        String fileKey = java.util.UUID.randomUUID().toString().replaceAll(RUNG, "");
        fileMap.put(fileKey, multipartFile);
        return fileKey;
    }
}